package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.DeptInfo;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.service.IDeptInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * <p>
 * 
 * @Author: Steve @Since： 2019-01-22
 */
@RestController
@RequestMapping("/sys/deptInfo")
@Slf4j
public class DeptInfoController {

	@Autowired
	private IDeptInfoService deptInfoService;


	/**
	 * 分页列表查询
	 * @param deptInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<DeptInfo>> queryPageList(DeptInfo deptInfo,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req) {
		Result<IPage<DeptInfo>> result = new Result<IPage<DeptInfo>>();
		QueryWrapper<DeptInfo> queryWrapper = QueryGenerator.initQueryWrapper(deptInfo, req.getParameterMap());
		Page<DeptInfo> page = new Page<DeptInfo>(pageNo, pageSize);
		IPage<DeptInfo> pageList = deptInfoService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	public Result<List<SysDepartTreeModel>> queryTreeList(@RequestParam(name = "ids", required = false) String ids) {
		Result<List<SysDepartTreeModel>> result = new Result<>();
		try {
			if(oConvertUtils.isNotEmpty(ids)){
				List<SysDepartTreeModel> departList = deptInfoService.queryTreeList(ids);
				result.setResult(departList);
			}else{
				List<SysDepartTreeModel> list = deptInfoService.queryTreeList(null);
				result.setResult(list);
			}
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 *   添加
	 * @param deptInfo
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DeptInfo deptInfo) {
		deptInfoService.save(deptInfo);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 * @param deptInfo
	 * @return
	 */
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DeptInfo deptInfo) {
		deptInfoService.updateById(deptInfo);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		deptInfoService.removeById(id);
		return Result.OK("删除成功!");
	}


}
